package com.abigdreamer.swing.app;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.abigdreamer.swing.app.skin.SkinConst;
import com.abigdreamer.swing.app.skin.SkinManager;
import com.abigdreamer.swing.app.skin.SkinMenu;
import com.abigdreamer.swing.component.UIUtil;
import com.sun.awt.AWTUtilities;

import craky.component.JImagePane;
import craky.componentc.CRootPaneUI;
import craky.componentc.JCFrame;
import craky.componentc.JCMenu;
import craky.layout.LineLayout;
import craky.util.Config;

public class SkinableWindow
  extends JCFrame
{
  private static final long serialVersionUID = -5862460651931105069L;
  private SkinMenu skinMenu;
  private Config config;
  protected JImagePane titleContent;
  
  public SkinableWindow()
  {
    this.config = new Config(SkinConst.CONFIG_PATH);
    
    setMinimumSize(new Dimension(800, 550));
    setTitleOpaque(Boolean.valueOf(this.config.getProperty("Title_Opaque", "false")).booleanValue());
    setImageDisplayMode(CRootPaneUI.ImageDisplayMode.valueOf(this.config.getProperty("Skin_Mode", "FILL")));
    setImageAlpha(Float.valueOf(this.config.getProperty("Skin_Alpha", "0.50")).floatValue());
    setSize(SwingUtil.stringToSize(this.config.getProperty("Win_Size", "900,600")));
    
    SkinManager.setOwner(this);
    
    this.titleContent = getTitleContentPane();
    if (UIUtil.isTranslucencySupported()) {
      AWTUtilities.setWindowOpacity(this, Float.valueOf(this.config.getProperty("Win_Alpha", "1.0")).floatValue());
    }
    addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        SkinableWindow.this.onWindowClosing();
      }
    });
    UIUtil.registerKeyEvent(this, new AbstractAction()
    {
      private static final long serialVersionUID = 2054393387947500297L;
      
      public void actionPerformed(ActionEvent e)
      {
        SkinableWindow.this.onF5();
      }
    }, "F5", 116, 0);
    
    UIUtil.registerKeyEvent(this, new AbstractAction()
    {
      private static final long serialVersionUID = 7021461035699115902L;
      
      public void actionPerformed(ActionEvent e)
      {
        SkinableWindow.this.onCtrlEnter();
      }
    }, "Ctrl+Enter", 10, 2);
    
    init();
    initUI();
  }
  
  protected void onCtrlEnter() {}
  
  protected void onF5() {}
  
  protected void onWindowClosing() {}
  
  private void init()
  {
    this.titleContent.setLayout(new LineLayout(0, 0, 0, 2, 0, 11, 10, 0));
  }
  
  public void showWindow()
  {
    setVisible(true);
  }
  
  protected void initUI()
  {
    setDefaultCloseOperation(0);
    if (Boolean.parseBoolean(this.config.getProperty("Win_Maximized", "false"))) {
      setExtendedState(6);
    }
    setSize(initSize());
    
    setMinimumSize(initMinimumSize());
    
    setIconImage(initIconImage());
    
    JPanel content = (JPanel)getContentPane();
    content.setLayout(new BorderLayout());
    content.setBorder(new EmptyBorder(0, 2, 5, 2));
    
    setLocationRelativeTo(null);
    
    setTitleOpaque(initTitleOpaque());
    
    setImageDisplayMode(initImageDisplayMode());
    
    setImageAlpha(initImageAlpha());
  }
  
  public void setVisible(boolean b)
  {
    initMenu();
    super.setVisible(b);
  }
  
  private void initMenu()
  {
    if (this.skinMenu != null) {
      return;
    }
    JMenuBar bar = new JMenuBar();
    bar.setOpaque(false);
    bar.setBorder(new EmptyBorder(0, 0, 0, 0));
    bar.setBorderPainted(false);
    bar.setLayout(new GridLayout(1, 2));
    bar.setPreferredSize(new Dimension(40, 20));
    bar.setFocusable(false);
    
    List<JCMenu> menuList = createMenu();
    if (menuList != null) {
      for (JCMenu jMenu : menuList) {
        bar.add(jMenu);
      }
    }
    bar.add(this.skinMenu = new SkinMenu());
    this.titleContent.add(bar, "End");
    resetSkinPopupLocation();
    
    this.skinMenu.setVisible(this.skinMenuVisible);
  }
  
  protected boolean skinMenuVisible = false;
  
  public void setSkinMenuVisible(boolean v)
  {
    this.skinMenuVisible = v;
  }
  
  protected List<JCMenu> createMenu()
  {
    return null;
  }
  
  public void resetSkinPopupLocation()
  {
    this.skinMenu.setMenuLocation(Integer.parseInt(SkinManager.getProperty("Skin_Popup_X", "-216")), this.skinMenu.getPreferredSize().height);
  }
  
  protected Dimension initSize()
  {
    return new Dimension(800, 600);
  }
  
  protected Dimension initMinimumSize()
  {
    return new Dimension(100, 100);
  }
  
  protected Image initIconImage()
  {
    return null;
  }
  
  protected boolean initTitleOpaque()
  {
    return false;
  }
  
  protected CRootPaneUI.ImageDisplayMode initImageDisplayMode()
  {
    return CRootPaneUI.ImageDisplayMode.FILL;
  }
  
  protected float initImageAlpha()
  {
    return 0.5F;
  }
}
